﻿using Asp.Versioning;
using FlyingEye.Factories;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace FlyingEye.Controllers
{
    [Route("api/device-ops-service/v{version:apiVersion}/production-workshops")]
    [ApiVersion("1.0")]
    [ControllerName("ProductionWorkshop")]
    [Authorize]
    public class ProductionWorkshopController : DefaultController
    {
        private readonly IProductionWorkshopRepository _productionWorkshopRepository;

        private readonly IProductionProcessRepository _productionProcessRepository;

        public ProductionWorkshopController(
            IProductionWorkshopRepository factoryBaseRepository,
            IProductionProcessRepository productionProcessRepository)
        {
            _productionWorkshopRepository = factoryBaseRepository;
            _productionProcessRepository = productionProcessRepository;
        }

        [HttpGet("{id}", Name = "GetProductionWorkshopId")]
        public async Task<ActionResult<ProductionWorkshopResponse>> GetAsync(Guid id)
        {
            return await this.GetInvokeAsync(async () =>
            {
                var factoryBase = await _productionWorkshopRepository.GetAsync(id);
                return ObjectMapper.Map<ProductionWorkshopModel, ProductionWorkshopResponse>(factoryBase);
            });
        }

        [HttpGet]
        public async Task<ActionResult<List<ProductionWorkshopResponse>>> GetListAsync()
        {
            return await this.GetInvokeAsync(async () =>
            {
                var factoryBases = await _productionWorkshopRepository.GetListAsync();
                return ObjectMapper.Map<List<ProductionWorkshopModel>, List<ProductionWorkshopResponse>>(factoryBases);
            });
        }

        [HttpGet("{id}/production-processes")]
        public async Task<ActionResult<List<ProductionProcessResponse>>> GetProductionProcessesAsync(Guid id)
        {
            return await this.GetInvokeAsync(async () =>
            {
                var productionProcesses = await _productionProcessRepository.GetListAsync(item => item.ProductionWorkshopId == id);
                return ObjectMapper.Map<List<ProductionProcessModel>, List<ProductionProcessResponse>>(productionProcesses);
            });
        }

        [HttpPost]
        public async Task<ActionResult<ProductionWorkshopResponse>> InsertAsync([FromBody] CreateProductionWorkshopRequest request)
        {
            return await this.InsertInvokeAsync(async () =>
            {
                var factoryBase = ObjectMapper.Map<CreateProductionWorkshopRequest, ProductionWorkshopModel>(request);
                var insertedFactoryBase = await _productionWorkshopRepository.InsertAsync(factoryBase);
                return ObjectMapper.Map<ProductionWorkshopModel, ProductionWorkshopResponse>(insertedFactoryBase);
            }, "GetProductionWorkshopId", e => new { e.Id });
        }

        [HttpPut]
        public async Task<ActionResult<ProductionWorkshopResponse>> UpdateAsync([FromBody] UpdateProductionWorkshopRequest request)
        {
            return await this.PutInvokeAsync(async () =>
            {
                var factoryBase = ObjectMapper.Map<UpdateProductionWorkshopRequest, ProductionWorkshopModel>(request);
                var updatedFactoryBase = await _productionWorkshopRepository.UpdateAsync(factoryBase);
                return ObjectMapper.Map<ProductionWorkshopModel, ProductionWorkshopResponse>(updatedFactoryBase);
            });
        }

        [HttpDelete("{id}")]
        public async Task<NoContentResult> DeleteAsync(Guid id)
        {
            return await this.DeleteInvokeAsync(async () =>
            {
                await _productionWorkshopRepository.DeleteAsync(id);
            });
        }
    }
}
